<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/4/24
 * Time: 21:27
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use app\home\model\Test;
use app\home\model\Fenxiao;
use app\home\model\Gate;
use app\home\model\Stage;
class Scan extends Base
{
    /**
     * 测试列表
     */
    public function test(){
        $userId = input('openid', session('userId'));
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $code = Test::where(['status' => 0])->group('code')->order('test_time','desc')->limit(20)->field('code,test_time')->select();
        $arr = [];
        foreach ($code as $key => $value){
            $tem = [];
            $tem['code'] = $value['code'];
            $tem['time'] = date('Y-m-d',$value['test_time']);
            $tem['class'] = '暂无';  // 阶段
            $tem['class1'] = '暂无';  // 大类
            $tem['class2'] = '暂无';  // 项目大类
            $tem['class3'] = '暂无';  // 一级项目
            $tem['class4'] = '暂无';  // 二级项目
            $paper = Fenxiao::where('paper_id',$value['code'])->find();
            if ($paper){
                $stage = Stage::where('id',$paper['stage_id'])->value('name');
                $tem['class'] = iconv("gbk", "utf-8//ignore", $stage);  // 阶段
                $gate = Gate::where('id',$paper['project_id'])->field('class1,class2,class3,class4')->find();
                $tem['class1'] = iconv("gbk", "utf-8//ignore", $gate['class1']);  // 大类
                $tem['class2'] = iconv("gbk", "utf-8//ignore", $gate['class2']);  // 项目大类
                $tem['class3'] = iconv("gbk", "utf-8//ignore", $gate['class3']);  // 一级项目
                $tem['class4'] = iconv("gbk", "utf-8//ignore", $gate['class4']);  // 二级项目
            }
            array_push($arr,$tem);
        }
        $this->assign('list',$arr);
        return $this->fetch();
    }
    /**
     * 测试列表 更多
     */
    public function testmore(){
        $len = input('length');
        $code = Test::where(['status' => 0])->group('code')->order('test_time','desc')->limit($len,20)->field('code,test_time')->select();
        $arr = [];
        foreach ($code as $key => $value){
            $tem = [];
            $tem['code'] = $value['code'];
            $tem['time'] = date('Y-m-d',$value['test_time']);
            $tem['class'] = '暂无';  // 阶段
            $tem['class1'] = '暂无';  // 大类
            $tem['class2'] = '暂无';  // 项目大类
            $tem['class3'] = '暂无';  // 一级项目
            $tem['class4'] = '暂无';  // 二级项目
            $paper = Fenxiao::where('paper_id',$value['code'])->find();
            if ($paper){
                $stage = Stage::where('id',$paper['stage_id'])->value('name');
                $tem['class'] = iconv("gbk", "utf-8//ignore", $stage);  // 阶段
                $gate = Gate::where('id',$paper['project_id'])->field('class1,class2,class3,class4')->find();
                $tem['class1'] = iconv("gbk", "utf-8//ignore", $gate['class1']);  // 大类
                $tem['class2'] = iconv("gbk", "utf-8//ignore", $gate['class2']);  // 项目大类
                $tem['class3'] = iconv("gbk", "utf-8//ignore", $gate['class3']);  // 一级项目
                $tem['class4'] = iconv("gbk", "utf-8//ignore", $gate['class4']);  // 二级项目
            }
            array_push($arr,$tem);
        }
        if ($arr){
            return $this->success('加载成功','',$arr);
        }else{
            return $this->error('加载失败');
        }
    }
    /**
     * 测试排名
     */
    public function detail(){
        $userId = input('openid', session('userId'));
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $code = input('code');
        $list = Test::where(['code' => $code,'status' => 0])->order('score','desc')->field('openid,score')->limit(30)->select();
        foreach ($list as $key => $value){
            $User = WechatUser::where(['openid' => $value['openid']])->field('name,school_id,headimgurl,order_id,review_status,unsubscribe_time,subscribe')->find();
            if ($User['review_status'] == 0 || !empty($User['unsubscribe_time']) || $User['subscribe'] == 0){
                unset($list[$key]);
            }else{
                $value['name'] = $User['name']?$User['name']:"暂无";
                $value['school'] = $User['school_id'];
                $value['headimgurl'] = $User['headimgurl'];
                $value['order'] = $User['order_id'];
            }
        }
        $this->assign('sign',$list);
        $this->assign('code',$code);
        return $this->fetch();
    }
    /**
     * 更多
     */
    public function more(){
        $code = input('code');
        $length = input('length');
        $list = Test::where(['code' => $code,'status' => 0])->order('score','desc')->field('openid,score')->limit($length,30)->select();
        foreach ($list as $key => $value){
            $User = WechatUser::where(['openid' => $value['openid']])->field('name,school_id,headimgurl,order_id,review_status,unsubscribe_time,subscribe')->find();
            if ($User['review_status'] == 0 || !empty($User['unsubscribe_time']) || $User['subscribe'] == 0){
                unset($list[$key]);
            }else{
                $value['name'] = $User['name']?substr($User['name'],0,3)."同学":"暂无";
                $value['school'] = get_school($User['school_id']);
                $value['headimgurl'] = $User['headimgurl'];
                $value['order'] = $User['order_id'];
                $value['len'] = $length;
            }
        }
        if ($list){
            return $this->success('加载成功','',$list);
        }else{
            return $this->error('加载失败');
        }
    }
}